
//
//  GKLHelpCenterViewController.m
//  jinwan
//
//  Created by GKiOS on 2017/6/6.
//  Copyright © 2017年 wy. All rights reserved.
//

#import "GKLHelpCenterViewController.h"
#import <MJExtension/MJExtension.h>
#import "GKLHelpCenterModel.h"
#import "GKLHelpCenterTableHeader.h"
#import "NetworkManager.h"
#import "GKLHelpCenterUrl.h"
#import "GKLHelpCenterCell.h"
//#import "GKLFeedbackViewController.h"
#import "GKHelpTitleCell.h"
#import <UITableView+FDTemplateLayoutCell.h>
static NSString *const cellID = @"help.center.cell.id";
static NSString *const headerID = @"help.center.header.id";
static NSString *const tittleCellID = @"help.title.cell.id";

@interface GKLHelpCenterViewController ()
@property(nonatomic, assign) NSUInteger   page;
@end

@implementation GKLHelpCenterViewController

- (instancetype)init
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self confingureRefresh];
    self.title = @"帮助中心";
    [self beginHeaderRefreshing];
    [self confingureNav];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

#pragma mark - nav 

- (void)confingureNav
{
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"意见反馈" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction:)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

- (void)rightBarButtonItemAction:(UIBarButtonItem *)sender
{
//    GKLFeedbackViewController *explainVC = [[GKLFeedbackViewController alloc] init];
//    [self.navigationController pushViewController:explainVC animated:YES];
}

#pragma mark - confingureTable

- (void)confingureTableView
{
    [super confingureTableView];
    self.tableView.backgroundColor = [UIColor custom_color:@"#F7F7F7"];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
    [self.tableView registerClass:[GKLHelpCenterCell  class] forCellReuseIdentifier:cellID];
    [self.tableView registerClass:[GKHelpTitleCell class] forCellReuseIdentifier:tittleCellID];
    [self.tableView registerClass:[GKLHelpCenterTableHeader class] forHeaderFooterViewReuseIdentifier:headerID];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
}

#pragma mark - refresh

- (void)refreshTable
{
    if (!self.dataArray.count) {
        [self beginHeaderRefreshing];
    }
}

- (void)confingureRefresh
{
    [super confingureRefresh];
}

- (void)loadNew
{
    self.page = 1;
    [self handleHelpCenterRequestWithPage:self.page];
    
}

- (void)loadMore
{
    if (!self.dataArray.count) {
        [self endFooterRefresh];
        return;
    }
    self.page = self.page + 1;
    [self handleHelpCenterRequestWithPage:self.page];
}


#pragma mark - net

- (void)handleHelpCenterRequestWithPage:(NSUInteger)page
{
    NSDictionary *parames = @{@"pager.pageNumber":@(page),
                              @"pager.pageSize":@20,
                              @"way":@3};
    __weak typeof(self) weakSelf = self;
    [[NetworkManager defaultManager] getWithURLString:[GKLHelpCenterUrl helpCenterUrl] parameters:parames success:^(id responseObject) {
        [self endFooterRefresh];
        [self endHeaderRefresh];
        if ([responseObject[@"rcd"] isEqualToString:kNetResponse_R0001]) {
            [weakSelf handleResponseData:responseObject];
        }
    } failure:^(NSError *error) {
        [self endFooterRefresh];
        [self endHeaderRefresh];
    }];

    
}

- (void)handleResponseData:(NSDictionary *)responseObject
{
    if (self.isHeaderRefreshing) {
        [self.dataArray removeAllObjects];
    }
    NSMutableArray *modelArray = [GKLHelpCenterModel mj_objectArrayWithKeyValuesArray:responseObject[@"articleItemList"]];
    if (modelArray.count) {
        [self.dataArray addObjectsFromArray:modelArray];
    }
    NSUInteger pageTotal = [[responseObject[@"pageBean"] valueForKey:@"totalCount"] integerValue];
    
    [self reloadTableWithPageTotal:pageTotal];
    
}

#pragma mark - table delete

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    GKLHelpCenterModel *model = self.dataArray[section];
    if (model.isOpen) {
        return 2;
    } else{
        return 1;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        GKHelpTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:tittleCellID forIndexPath:indexPath];
        cell.model = self.dataArray[indexPath.section];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        __weak typeof(self) weakSelf = self;
        cell.callBack = ^{
            GKLHelpCenterModel *model = self.dataArray[indexPath.section];
            model.isOpen = !model.isOpen;
            [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
        };
        return cell;
    } else {
        GKLHelpCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
        cell.model = self.dataArray[indexPath.section];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 50;
    }
    __weak typeof(self) weakSelf = self;
    return [tableView fd_heightForCellWithIdentifier:cellID cacheByIndexPath:indexPath configuration:^(id cell) {
       
            GKLHelpCenterModel *model = weakSelf.dataArray[indexPath.section];
            GKLHelpCenterCell *temCell = (GKLHelpCenterCell *)cell;
            temCell.model = model;
    }];
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor custom_color:@"#F7F7F7"];
    return view;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor custom_color:@"#F7F7F7"];
    return view;
}

#pragma mark - empty table

- (UIImage *)tableViewEmptyPlaceHolderImage
{
    return [UIImage imageNamed:@"common_pic_record"];
}

- (NSAttributedString *)tableViewEmptyTitle
{
    NSAttributedString *attr = [[NSAttributedString alloc] initWithString:@"暂无记录" attributes:@{NSForegroundColorAttributeName:[UIColor custom_color:@"#BBBBBB"], NSFontAttributeName:[UIFont systemFontOfSize:14]}];
    return attr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
